package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.constant.Constant;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.ModuleGroup;
import com.rzjm.exercise.module.domain.WxModule;
import com.rzjm.exercise.module.service.ModuleGroupService;
import com.rzjm.exercise.module.service.WxModuleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 微信小程序模块
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2020-12-15 22:36:23
 */

@Controller
@RequestMapping("/module/wxmodule")
public class WxModuleController {
    @Resource
    private WxModuleService wxModuleService;
    @Resource
    private ModuleGroupService moduleGroupService;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Log("查询微信小程序模块页面")
    @GetMapping()
    @RequiresPermissions("module:wxmodule:wxmodule")
    String WxModule() {
        return "module/wxmodule/module";
    }

    @Log("查询微信小程序模块")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:wxmodule:wxmodule")
    public DataTableLayUI<WxModule> list(DataTableLayUI<WxModule> condition) {
        return wxModuleService.pageSearchLayUI(condition);
    }

    @Log("新增微信小程序模块页面")
    @GetMapping("/add")
    @RequiresPermissions("module:wxmodule:add")
    String add(Model model) {
        model.addAttribute("module", new WxModule());
        return "module/wxmodule/detail";
    }

    @Log("编辑微信小程序模块页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:wxmodule:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        WxModule wxmodule = wxModuleService.findById(id);
        model.addAttribute("module", wxmodule);
        return "module/wxmodule/detail";
    }


    @Log("编辑微信小程序模块页面")
    @GetMapping("/goBandModule/{groupId}")
    @RequiresPermissions("module:group:module")
    String goBandModule(@PathVariable("groupId") Integer groupId, Model model) {
        List<WxModule> moduleList = wxModuleService.selectAllList();
        if(moduleList !=null){
            for (WxModule wxModule : moduleList){
                wxModule.setIsCheck(0);
                QueryWrapper<ModuleGroup> qw = new QueryWrapper<>();
                qw.eq("module_id",wxModule.getId());
                qw.eq("group_id",groupId);
                ModuleGroup moduleGroup = moduleGroupService.getOne(qw);
                if(moduleGroup!=null){
                    wxModule.setIsCheck(1);
                }
            }
        }
        model.addAttribute("modules", moduleList);
        model.addAttribute("groupId", groupId);
        return "module/group/band";
    }

    /**
     * 保存
     */
    @Log("新增微信小程序模块")
    @ResponseBody
    @PostMapping("/editModuleGroup")
    @RequiresPermissions("module:group:module")
    public R editModuleGroup(@Valid ModuleGroup moduleGroup) {
        if(moduleGroup.getIsCheck() == 0){
            QueryWrapper<ModuleGroup> qw = new QueryWrapper<>();
            qw.eq("module_id",moduleGroup.getModuleId());
            qw.eq("group_id",moduleGroup.getGroupId());
            ModuleGroup group = moduleGroupService.getOne(qw);
            if(group!=null){
                if (moduleGroupService.removeById(group.getId())) {
                    return R.ok();
                }
            } else{
                return R.ok();
            }
        }else{
            QueryWrapper<ModuleGroup> qw = new QueryWrapper<>();
            qw.eq("module_id",moduleGroup.getModuleId());
            qw.eq("group_id",moduleGroup.getGroupId());
            ModuleGroup group = moduleGroupService.getOne(qw);
            if(group == null){
                if (moduleGroupService.save(moduleGroup)) {
                    return R.ok();
                }
            }else{
                return R.ok();
            }
        }
        return R.error();
    }


    /**
     * 保存
     */
    @Log("新增微信小程序模块")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:wxmodule:add")
    public R save(@Valid WxModule wxmodule) {
        if (wxModuleService.save(wxmodule)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新微信小程序模块")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:wxmodule:edit")
    public R update(@Valid WxModule wxmodule) {
        boolean bl = wxModuleService.updateById(wxmodule);
        if(bl){
            redisTemplate.opsForValue().set(Constant.PARAMETER,wxmodule);
        }
        return R.ok();
    }



}
